package com.qf.service.impl;

import com.qf.dao.UserDao;
import com.qf.dao.impl.UserDaoImpl;
import com.qf.entity.User;
import com.qf.service.UserService;
import com.qf.utils.CodeUtils;
import com.qf.utils.EmailUtils;
import com.qf.utils.MD5Utils;

/**
 * @author 10202
 * @version V1.0
 * @Project xiaomi
 * @Package com.qf.service.impl
 * @Date 2023/4/13 14:52
 */
public class UserServiceImpl implements UserService {
    UserDao userDao=new UserDaoImpl();
    @Override
    public void addUser(User user) {
        user.setCode(CodeUtils.createCode());
        user.setPassword(MD5Utils.md5(user.getPassword()));
        userDao.add(user);

        EmailUtils.sendEmail(user);
    }

    @Override
    public boolean queryByUsername(String username) {
        try {
            User user=userDao.selectByUsername(username);
            if (user!=null){
                return true;
            }else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public User login(String username, String password) {
        password = MD5Utils.md5(password);
        User user=userDao.login(username,password);
        if (user==null){
            throw new RuntimeException("用户名或者密码错误");
        }
        return user;
    }
}
